"""Property schemas for API validation and serialization."""

from __future__ import annotations

from datetime import date, datetime
from typing import Optional

from pydantic import BaseModel, Field, ConfigDict

from ..models.enums import PropertyStatus, PropertyType


class PropertyBase(BaseModel):
    """Base property schema with common fields."""

    community_name: str = Field(..., max_length=255, description="小区名称")
    address: str = Field(..., max_length=500, description="详细地址")
    room_number: str = Field(..., max_length=50, description="房号")
    layout: str = Field(..., max_length=50, description="户型(如: 2室1厅)")
    rent_price: int = Field(..., gt=0, description="租金(元/月)")
    landlord_phone: str = Field(..., max_length=20, description="房东电话")

    area: Optional[float] = Field(None, gt=0, description="面积(平方米)")
    floor_info: Optional[str] = Field(None, max_length=50, description="楼层信息")
    orientation: Optional[str] = Field(None, max_length=50, description="朝向")
    decoration: Optional[str] = Field(None, max_length=50, description="装修情况")
    landlord_name: Optional[str] = Field(None, max_length=100, description="房东姓名")
    status: PropertyStatus = Field(default=PropertyStatus.VACANT, description="房源状态")
    property_type: PropertyType = Field(default=PropertyType.APARTMENT, description="房源类型")
    available_date: Optional[date] = Field(None, description="可租日期")
    notes: Optional[str] = Field(None, description="备注(配套设施等)")


class PropertyCreate(PropertyBase):
    """Schema for creating a new property."""

    pass


class PropertyUpdate(BaseModel):
    """Schema for updating an existing property."""

    community_name: Optional[str] = Field(None, max_length=255)
    address: Optional[str] = Field(None, max_length=500)
    room_number: Optional[str] = Field(None, max_length=50)
    layout: Optional[str] = Field(None, max_length=50)
    rent_price: Optional[int] = Field(None, gt=0)
    landlord_phone: Optional[str] = Field(None, max_length=20)
    area: Optional[float] = Field(None, gt=0)
    floor_info: Optional[str] = Field(None, max_length=50)
    orientation: Optional[str] = Field(None, max_length=50)
    decoration: Optional[str] = Field(None, max_length=50)
    landlord_name: Optional[str] = Field(None, max_length=100)
    status: Optional[PropertyStatus] = None
    property_type: Optional[PropertyType] = None
    available_date: Optional[date] = None
    notes: Optional[str] = None


class PropertyOut(PropertyBase):
    """Schema for property output with all fields."""

    id: int
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class PropertyListOut(BaseModel):
    """Schema for paginated property list."""

    total: int
    items: list[PropertyOut]
    page: int
    page_size: int


class PropertyPhotoBase(BaseModel):
    """Base schema for property photo."""

    photo_url: str = Field(..., max_length=500, description="照片URL/路径")
    photo_type: Optional[str] = Field(None, max_length=50, description="照片类型")
    display_order: Optional[int] = Field(0, description="显示顺序")


class PropertyPhotoCreate(PropertyPhotoBase):
    """Schema for creating property photo."""

    property_id: int = Field(..., description="房源ID")


class PropertyPhotoOut(PropertyPhotoBase):
    """Schema for property photo output."""

    id: int
    property_id: int
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)
